﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManageSystemApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    public class SystemController : ControllerBase
    {
        readonly ISystemServices _systemServices;
        public SystemController(ISystemServices systemServices)
        {
            _systemServices = systemServices;
        }
        [HttpPost]
        [Route("GetAll")]
        public ApiResult GetAll(PageBo<SysUpgradeParams> pageBo)
        {
            return ApiResultStatus.Success(_systemServices.GetAll(pageBo));
        }
        [HttpGet]
        [Route("CheckUpdate")]
        public ApiResult CheckUpdate(string appName)
        {
            if (string.IsNullOrEmpty(appName))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            return ApiResultStatus.Success(_systemServices.CheckUpdate(appName));
        }
        [HttpPost]
        [Route("Create")]
        public ApiResult Create([FromBody] SysUpgradeDto sysUpgrade)
        {
            if (sysUpgrade == null || sysUpgrade.VersionInsideNo == 0 || string.IsNullOrWhiteSpace(sysUpgrade.AppName))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (_systemServices.ExistByAppNameAndInsideNo(sysUpgrade.AppName, sysUpgrade.VersionInsideNo))
            {
                return ApiResultStatus.Fail(ApiResultCodes.APPNAME_INSIDENO_EXIST);
            }

            bool r = _systemServices.Create(sysUpgrade);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }

        [HttpPost, Route("Del")]
        public ApiResult Del(uint id)
        {
            if (id < 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_systemServices.ExistId(id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            bool r = _systemServices.Del(id);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }
    }
}
